var _options = [
	"全部股票",
	"中证800",
	"中证流通",
	"近3日平均成交额前500名",
	"近3日平均成交额大于1亿",
	"近3日平均成交额大于5000万场内基金",
	"近3日跑赢大盘",
    "下个交易日除息",
    "下个交易日股东大会",
    "下个交易日除权"
]

// 指数
var _index = [
	["sh000986","中证全指能源指数"],
	["sh000987","中证全指原材料指数"],
	["sh000988","中证全指工业指数"],
	["sh000989","中证全指可选消费指数"],
	["sh000990","中证全指主要消费指数"],
	["sh000991","中证全指医药卫生指数"],
	["sh000992","中证全指金融地产指数"],
	["sh000993","中证全指信息技术指数"],
	["sh000994","中证全指电信业务指数"],
	["sh000995","中证全指公用事业指数"],
]

/*
{
day: "2017-03-09 10:45:00",
open: "41.170",
high: "41.260",
low: "41.160",
close: "41.240",
radio : (close - open)/open,
volume: "92200",
ma_price5: 41.324,
ma_volume5: 242366,
ma_price10: 41.324,
ma_volume10: 242366,
ma_price30: 41.324,
ma_volume30: 242366
}
*/


/*
function Clsextremum(arr){
	var min = 0, max = 0, close = 0, open = 0;
	if (arr.length){
		close = arr[0].close;
		open = arr[0].open;
	}
	
	for (var i = 0; i < arr.length; i++){
		var e = arr[i];
		if (e.close > close){
			close = e.close;
			max = i;
		}
		
		if (e.open < open){
			open = e.open;
			min = i;
		}
	}
	
	return {
		min : min, 
		max : max, 
		close : close, 
		open : open
	}
}
*/

function extremum(arr){
	var min = 0, max = 0, high = 0, low = 0, max_vol = 0, high_vol = 0;
	if (arr.length){
		high = arr[0].high;
		low = arr[0].low;
	}
	
	for (var i = 0; i < arr.length; i++){
		var e = arr[i];
		if (e.high > high){
			high = e.high;
			max = i;
		}
		
		if (e.low < low){
			low = e.low;
			min = i;
		}
		
		if (e.volume > high_vol){
			high_vol = e.volume;
			max_vol = i;
		}
	}
	
	return {
		min : min, 
		max : max, 
		high : high, 
		low : low,
		max_vol : max_vol,
		high_vol : high_vol
	}
}

function linear(arr, key){
	/*
	y = a * x + b
	a = sum (xi - xv)(yi - yv) / sum (xi - xv)(xi - xv)
	b = yv - a * xv
	*/
	var sum_x = 0;
	var sum_y = 0;
	for (var i = 0; i < arr.length; i++){
		sum_x += i;
		sum_y += arr[i][key];
	}
	var xv = sum_x / arr.length;
	var yv = sum_y / arr.length;
	
	var numerator = 0;
	var denominator = 0;
	
	for (var i = 0; i < arr.length; i++){
		var x = i - xv;
		var y = arr[i][key] - yv;
		numerator += x * y;
		denominator += x * x;
	}
	
	var slope = numerator / denominator;
	var diff = yv - (slope * xv);
	var next = slope * arr.length + diff;
	
	return {
		slope : slope,
		diff : diff,
		next : next
	}
}

var _filter = [
	{
		name : "全部",
		func : function(id, day, min, div, name){
			var len = day.length;
			/*
			{
			day: "2017-03-09 10:45:00",
			open: "41.170",
			high: "41.260",
			low: "41.160",
			close: "41.240",
			radio : (close - open)/open,
			volume: "92200",
			ma_price5: 41.324,
			ma_volume5: 242366,
			ma_price10: 41.324,
			ma_volume10: 242366,
			ma_price30: 41.324,
			ma_volume30: 242366
			}
			
			extremum {
				min : min, // 下标索引
				max : max, // 下标索引
				high : high, 
				low : low
			}
			*/
			return true;
		}
	},
	{
		name : "高开",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			var d1 = day[len-2];
			
			var diff = (cur.open - d1.close)*100/d1.close
			if (diff > 0.3){
				return diff;
			}
				
			return 0;
		}
	},
	{
		name : "低开",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			var d1 = day[len-2];
			
			var diff = (cur.open - d1.close)*100/d1.close
			if (diff < 0.3){
				return diff;
			}
				
			return 0;
		}
	},
	{
		name : "收盘上涨",
		func : function(id, day, min, div, name){
			var len = day.length;
			return day[len-1].close > day[len-1].open;
		}
	},
	/*
	{
		name : "收盘下跌",
		func : function(id, day, min, div, name){
			var len = day.length;
			return day[len-1].close < day[len-1].open;
		}
	},
	{
		name : "收盘价创20日新高",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			var m1 = Clsextremum(day.slice(-20));
			var m2 = Clsextremum(day.slice(-1));
			
			var AA1 = (cur.close  >= m1.close);
			var BB1 = cur.high > cur.open && cur.high > cur.close;
			
			if (AA1){
				return cur.high / cur.close;
			}
		}
	},
	*/
	{
		name : "今日创20日新高",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			var m1 = extremum(day.slice(-20));
			var m2 = extremum(day.slice(-1));
			
			var AA1 = (m2.high * 1.002 >= m1.high);
			var BB1 = cur.high > cur.open && cur.high > cur.close;
			
			if (AA1 && BB1){
				return m2.high / cur.close;
			}
		}
	},
	/*
	{
		name : "收盘价突破平台",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			
			var m1 = Clsextremum(day.slice(-21, -1));
			var m2 = Clsextremum(day.slice(-6, -2));
			
			var AA1 = cur.close == m1.close;
			var DD1 = cur.open < m1.close;
			
			if (AA1 && DD1){
				return m2.high / cur.close;
			}
		}
	},
	*/
	{
		name : "长上影线",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			var d1 = day[len-2];			
			var max = (cur.close > cur.open ? cur.close : cur.open);
			var min = (cur.close < cur.open ? cur.close : cur.open);			
			var BB1 = (cur.high - max) * 100 / d1.close > 1.5;
			var CC1 = (cur.high - max) > 2 * (min - cur.low);
			var DD1 = (cur.high - max) > Math.abs(cur.close - cur.open);
			if (BB1 && CC1 && DD1){
				return cur.high / cur.close;
			}
		}
	},
	{
		name : "收盘新高",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			var last = day[len-2];
			var m1 = extremum(day.slice(-21, -1));
			var m2 = extremum(day.slice(-22, -2));
			
			return (last.close < m2.high) && (cur.close > m1.high)
		}
	},
	/*
	{
		name : "宽条件昨天大涨今天缩量 ",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			var d1 = day[len-2];
			var d2 = day[len-3];
			
			var m1 = extremum(day.slice(-20));
			var m2 = extremum(day.slice(-2));
			
			var AA1 = (m2.high * 1.002 >= m1.high);
			var BB1 = cur.high > cur.open && cur.high > cur.close;

			
			var DD1 = d1.volume / cur.volume > 1;
			var EE1 = ((d1.close - d1.open)* 100 / d2.close > 3) && ((d1.close - d2.close)* 100 / d2.close < 10);
			
			if (  AA1 && BB1 && DD1 && EE1 ){
				return m1.high / cur.close;
			}
		}
	},
	{
		name:"昨天大涨今天缩量",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			var d1 = day[len-2];
			var d2 = day[len-3];
			
			var m1 = extremum(day.slice(-21, -1));
			
			var AA1 = (d1.high >= m1.high);
			var BB1 = ((cur.close - cur.open)* 100 / d1.close < 3) && ((cur.close - d1.close)* 100 / d1.close < 3);
			var DD1 = d1.volume / cur.volume > 1;
			var EE1 = ((d1.close - d1.open)* 100 / d2.close > 3) && ((d1.close - d2.close)* 100 / d2.close < 10);
			var FF1 = (cur.high - cur.close) > (cur.open - cur.low);
			
			if (AA1 && BB1 && DD1 && EE1 && FF1){
				return m1.high / cur.close;
			}
		}
	},
	*/
	{
		name : "缩量",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			
			if (cur.volume * 1.6 < cur.ma_volume5) {
				return cur.radio;
			}
			
			return 0;
		}
	},
	{
		name : "放量",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];

			if (cur.volume > cur.ma_volume5 * 1.6){
				return cur.radio;
			}
			
			return 0;
		}
	},
	{
		name : "昨天新高",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			var m = extremum(day.slice(-22, -2));
			
			if (m.max >= 19) {
				return cur.radio;
			}
			
			return 0;
		}
	},
	{
		name : "昨天新低",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			var m = extremum(day.slice(-22, -2));
			
			if (m.min >= 19) {
				return cur.radio;
			}
			
			return 0;
		}
	},
	{
		name : "连续上涨",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			var d1 = day[len-2];
			var d2 = day[len-3];
		
			if ((cur.high > d1.high)
				&& (d1.high > d2.high)
				&& (cur.low > d1.low)
				&& (d1.low > d2.low)){
				return cur.radio;
			}
			
			return 0;
		}
	},
	{
		name : "大红柱",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			
			if ((cur.high / cur.close < 1 + (cur.radio / 2))){
				return cur.radio;
			}
			
			return 0;
		}
	},
	{
		name : "穿越5日均线",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			
			if ((cur.ma_price5 > cur.low) && (cur.ma_price5 < cur.high)){
				return cur.radio;
			}
			
			return 0;
		}
	},
	{
		name : "穿越10日均线",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			
			if ((cur.ma_price10 > cur.low) && (cur.ma_price10 < cur.high)){
				return cur.radio;
			}
			
			return 0;
		}
	},
	{
		name : "穿越30日均线",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			
			if ((cur.ma_price30 > cur.low) && (cur.ma_price30 < cur.high)){
				return cur.radio;
			}
			
			return 0;
		}
	},
	{
		name : "突破放量高价",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			var last = day.slice(-21, -1);
			var m = extremum(last);
			/*
			var high = 0;
			for (var i = 0; i < last.length - 1; i++){
				var c = last[last.length - 1 - i];
				if (c.volume > c.ma_volume5 * 1.6){
					high = c.high;
					break;
				}
			}
			
			if ((high > 0) && (cur.high >= high)){
				return cur.radio;
			}
			*/
			if ((m.high_vol > last[m.max_vol].ma_volume5 * 1.6)
				&& (cur.high >= last[m.max_vol].high))
			{
				return cur.radio;
			}
			
			return 0;
		}
	},
	{
		name : "线性回归涨",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			var last = day.slice(-5);
			//var m = extremum(last);
			
			var h = linear(last, 'high');
			var l = linear(last, 'low');
			
			if ((l.slope > 0)
				&& (h.slope >= l.slope))
			{
				return cur.radio;
			}
			
			return 0;
		}
	},
	{
		name : "盈亏比",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			var last = day.slice(-3);
			
			var h = linear(last, 'high');
			var l = linear(last, 'low');
			var c = linear(last, 'close');
			
			//var p = ((h.slope + l.slope)/(h.diff + l.diff)) + 0.5;
			var p = ((c.slope)/(c.diff)) + 0.5;
			var r = ((h.next - cur.close) / (cur.close - l.diff)) + 1;
			var ans = (p * (r + 1) - 1)
			
			console.log([id, h, l, p, r, ans]);
			
			/*
			p * w - (1-p) * s
			p * r * s  - (1-p)*s
			s * (p * r - 1 + p) = s * (p * (r + 1) - 1)
			*/
			if (ans > 1){
				return ans;
			}
			
			return 0;
		}
	},
	{
		name : "非涨停",
		func : function(id, day, min, div, name){
			var len = day.length;
			var cur = day[len-1];
			
			if (cur.radio < 0.095){
				return cur.radio;
			}
			
			return 0;
		}
	}
]